package com.liuche.pictureserver.process.picture;

import com.liuche.pictureserver.common.exception.ErrorCode;
import com.liuche.pictureserver.pojo.dto.PictureSaveDTO;
import com.liuche.pictureserver.process.ProcessContext;
import com.liuche.pictureserver.process.ProcessHandler;
import com.liuche.pictureserver.utils.MinioUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 图片保存处理逻辑
 *
 * @author liuche
 */
@Slf4j
@Component
public class PictureUploadProcess extends ProcessHandler {
    @Resource
    private MinioUtil minioUtil;

    /**
     * 处理逻辑
     * @param context 上下文信息
     */
    @Override
    public void run(ProcessContext<?> context) {
        PictureSaveDTO model = (PictureSaveDTO) context.getModel();
        try {
            // 上传至Minio
            String fileUrl = minioUtil.uploadFile(model.getFile());
            model.setUrl(fileUrl);
        } catch (Exception e) {
            log.error("图片保存失败", e);
            context.setErrorCode(ErrorCode.UPLOAD_PICTURE_ERROR);
            context.setBreakFlag(true);
        }
        log.info("图片保存成功");
    }
}
